package com.et.core.util

import android.text.TextUtils
import androidx.recyclerview.widget.DiffUtil
import com.et.core.goods.GoodsInfo

class GoodsDiffCallBack(
    private val src: MutableList<GoodsInfo>,
    private val dest: MutableList<GoodsInfo>
) :
    DiffUtil.Callback() {
    override fun getOldListSize(): Int = src.size

    override fun getNewListSize(): Int = dest.size

    override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        if (oldItemPosition < src.size && newItemPosition < dest.size) {
            return TextUtils.equals(src[oldItemPosition].barcode, dest[newItemPosition].barcode)
        }
        return false
    }


    override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        if (oldItemPosition < src.size && newItemPosition < dest.size) {
            val oldItem = src[oldItemPosition]
            val newItem = dest[newItemPosition]
            return TextUtils.equals(oldItem.name, newItem.name)
                    && TextUtils.equals(oldItem.image, newItem.image)
                    && oldItem.discount == newItem.discount
                    && oldItem.channel.box == newItem.channel.box
                    && oldItem.channel.channel == newItem.channel.channel
                    && oldItem.channel.price == newItem.channel.price
        }
        return false
    }

}